package com.srwl.mytx.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.srwl.mytx.R;
import com.srwl.mytx.domain.SearchDataItem;

import java.util.List;

public class SearchHistoryAdapter extends RecyclerView.Adapter<SearchHistoryAdapter.MyViewHolder> {

    private static final String TAG = "SearchHistoryAdapter";
    private static final int ITEM_TYPE_SEARCH_HISTORY_TITLE = 0;
    private static final int ITEM_TYPE_SEARCH_HOTSPOT_TITLE = 1;
    private static final int ITEM_TYPE_SEARCH_CONTENT = 2;

    private List<SearchDataItem> searchSumList;     //历史搜索和热搜的数据集合
    private Context context;
    private SearchHistoryItemClickListener itemClickListener;
    private SearchHistoryClearAll searchHistoryClearAll;


    public SearchHistoryAdapter(Context context, List<SearchDataItem> searchSumList) {
        this.searchSumList = searchSumList;
        this.context = context;
    }


    @Override
    public int getItemViewType(int position) {
        Integer type = searchSumList.get(position).getType();
        if (type == SearchDataItem.SEARCH_HISTORY_ITEM_TYPE_LOCAL_CONTENT
                || type == SearchDataItem.SEARCH_HISTORY_ITEM_TYPE_REMOTE_CONTENT) {
            return ITEM_TYPE_SEARCH_CONTENT;
        } else if (type == SearchDataItem.SEARCH_HISTORY_ITEM_TYPE_TITLE_SEARCH_HISTORY) {
            return ITEM_TYPE_SEARCH_HISTORY_TITLE;
        } else {
            return ITEM_TYPE_SEARCH_HOTSPOT_TITLE;
        }
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_SEARCH_HOTSPOT_TITLE) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_hotspot_title_item_layout, parent, false);
            return new MyViewHolder(view);
        } else if (viewType == ITEM_TYPE_SEARCH_HISTORY_TITLE) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_history_title_item_layout, parent, false);
            return new MyViewHolder(view);
        } else {
            //其它则为数据类型，非标题类型
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_history_content_item_layout, parent, false);
            return new MyViewHolder(view);

        }
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        //判断搜索历史数据大于10个字符就将其后所有字符替换为"..."
        String content = searchSumList.get(position).getContent();
        if (content.length() > 10) {
            content = content.substring(0, 9) + "...";
        }

        if (searchSumList.get(position).getType() == SearchDataItem.SEARCH_HISTORY_ITEM_TYPE_TITLE_SEARCH_HOTSPOT) {
            Log.d(TAG, "onBindViewHolder: 热点搜索标题内容绑定执行了...");
            //绑定热门搜索标题的显示内容
            //holder.itemView.setBackgroundColor(context.getResources().getColor(R.color.transparent));
            holder.tv_searchContent.setText(content);
            return;
        }

        if (searchSumList.get(position).getType() == SearchDataItem.SEARCH_HISTORY_ITEM_TYPE_TITLE_SEARCH_HISTORY) {
            Log.d(TAG, "onBindViewHolder: 历史搜索标题内容控绑定行了...");
            //绑定历史搜索标题的显示内容
            holder.tv_searchContent.setText(content);

            //给当前itemView中的清除所有搜索记录的图标绑定单击事件
            holder.clearAll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //执行逻辑回调到外面处理
                    searchHistoryClearAll.clearAll(position);
                }
            });
        }

        if (searchSumList.get(position).getType() == SearchDataItem.SEARCH_HISTORY_ITEM_TYPE_LOCAL_CONTENT) {
            //绑定历史搜索数据
            holder.itemView.setBackground(context.getResources().getDrawable(R.drawable.search_view_gray_shape));
            holder.tv_searchContent.setTextSize(14);
            holder.tv_searchContent.setText(content);

            //给当前itemView绑定单击事件
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null) {
                        itemClickListener.onItemClick(position);
                    }
                }
            });
            //给当前itemView绑定长按事件
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {

                    if (itemClickListener != null) {
                        itemClickListener.onItemLongClick(position);
                    }
                    return true;
                }
            });
            return;
        }

        if (searchSumList.get(position).getType() == SearchDataItem.SEARCH_HISTORY_ITEM_TYPE_REMOTE_CONTENT) {
            //绑定推荐搜索数据
            holder.itemView.setBackground(context.getResources().getDrawable(R.drawable.search_view_gray_shape));
            holder.tv_searchContent.setText(content);
            //给当前item绑定单击事件
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null) {
                        itemClickListener.onItemClick(position);
                    }
                }
            });
        }


    }


    @Override
    public int getItemCount() {
        if (searchSumList.size() > 0) {
            return searchSumList.size();
        }
        return 0;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_searchContent;
        public ProgressBar progressBar;
        ImageView clearAll;

        private MyViewHolder(View itemView) {
            super(itemView);
            tv_searchContent = itemView.findViewById(R.id.tv_search_content);
            progressBar = itemView.findViewById(R.id.progress_bar);
            clearAll = itemView.findViewById(R.id.iv_clearAll);
        }
    }

    public void setSearchHistoryItemClickListener(SearchHistoryItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public interface SearchHistoryItemClickListener {
        void onItemClick(int position);

        void onItemLongClick(int position);
    }


    public void setSearchHistoryClearAll(SearchHistoryClearAll searchHistoryClearAll) {
        this.searchHistoryClearAll = searchHistoryClearAll;
    }

    /*
    清除全部历史搜索数据的回调接口
     */
    public interface SearchHistoryClearAll {
        void clearAll(int position);
    }
}
